/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.admi;

import controlador.ServletHelper;
import controlador.productos.ListarProducto;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import modelo.Productos;
import persistencia.ProductoBean;

/**
 *
 * @author Felipe
 */
@WebServlet(name = "StockBajos", urlPatterns = {"/StockBajos"})
public class StockBajos extends HttpServlet {

    @Resource(name = "jdbc/HOLAJDBC")
    private DataSource ds;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        Connection conexion = null;
        boolean condi = ServletHelper.validarUsu(request, response);
        if (condi) {
            try {

                conexion = ds.getConnection();

                ProductoBean bean;
                bean = new ProductoBean(conexion);

                Collection<Productos> lista = bean.stockBajos();

                if (null == lista) {
                    request.setAttribute("Mensaje", "NO existen Productos Bajos de Stock");
                    request.getRequestDispatcher("TT2/index.jsp").forward(request, response);
                } else {
                    request.setAttribute("lista", lista);
                    request.getRequestDispatcher("/Informes/StockBajos.jsp").forward(request, response);
                }
            } catch (SQLException ex) {
                Logger.getLogger(ListarProducto.class.getName()).log(Level.SEVERE,
                        "No se pudo obtener la conexión a la base de datos", ex);
                request.setAttribute("Mensaje", "No se pudo obtener la conexión a la base de datos");
                request.getRequestDispatcher("/Productos/formConsultarAllProducto.jsp").forward(request, response);
            } finally {
                try {
                    conexion.close();
                } catch (Exception noGestionar) {
                }
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

}
